package com.gun3y.entity.pattern;

import org.apache.commons.lang3.StringUtils;

public enum ExpressionType {
    ExactMatch {
	@Override
	public boolean isValid(String main, String value) {
	    return main.equals(value);
	}
    },
    StartWith {
	@Override
	public boolean isValid(String main, String value) {
	    return main.startsWith(value);
	}
    },
    EndWith {
	@Override
	public boolean isValid(String main, String value) {
	    return main.endsWith(value);
	}
    },
    Include {
	@Override
	public boolean isValid(String main, String value) {
	    return main.contains(value);
	}
    },
    Exclude {
	@Override
	public boolean isValid(String main, String value) {
	    return !main.contains(value);
	}
    },
    RegEx {
	@Override
	public boolean isValid(String main, String value) {
	    return main.matches(value);
	}
    },
    Split {
	@Override
	public boolean isValid(String main, String value) {
	    return false;
	}
    };

    public static ExpressionType[] getFilters() {
	return new ExpressionType[] { ExactMatch, StartWith, EndWith, Include, Exclude, RegEx };
    }

    public static ExpressionType[] getFunctions() {
	return new ExpressionType[] { Split, StartWith, EndWith, RegEx };
    }

    public static ExpressionType getExpressionType(String str) {
	if (StringUtils.isNotBlank(str)) {
	    try {
		return Enum.valueOf(ExpressionType.class, str);
	    }
	    catch (Exception e) {
		return ExpressionType.ExactMatch;
	    }
	}
	return ExpressionType.ExactMatch;
    }

    public abstract boolean isValid(String main, String value);

}
